package com.mall.user.services;

import com.alibaba.fastjson.JSON;
import com.mall.user.ILoginService;
import com.mall.user.IVerifyService;
import com.mall.user.constants.SysRetCodeConstants;
import com.mall.user.dal.entitys.Member;
import com.mall.user.dal.entitys.UserVerify;
import com.mall.user.dal.persistence.MemberMapper;
import com.mall.user.dal.persistence.UserVerifyMapper;
import com.mall.user.dto.UserLoginRequest;
import com.mall.user.dto.UserLoginResponse;
import com.mall.user.utils.JwtTokenUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.DigestUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Component
@Service
public class LoginServiceImpl implements ILoginService {
    @Autowired
    IVerifyImpl iVerify;

    @Autowired
    private UserVerifyMapper userVerifyMapper;

    @Autowired
    private MemberMapper memberMapper;

    @Override
    public UserLoginResponse longin(UserLoginRequest request) {
        UserLoginResponse userLoginResponse = new UserLoginResponse();
        Example example = new Example(Member.class);
        Example.Criteria criteria = example.createCriteria().andEqualTo("username", request.getUserName());
        String password = DigestUtils.md5DigestAsHex(request.getPassword().getBytes());
        criteria.andEqualTo("password",password);
        List<Member> members = memberMapper.selectByExample(example);
        if (members.isEmpty()){
            return null;
        }
        Member member = members.get(0);

        //判断用户是否激活
        if (member.getIsVerified() == "N"){
            //没有激活再发激活链接邮件
            Example example1 = new Example(UserVerifyMapper.class);
            example.createCriteria().andEqualTo("username",member.getUsername());
            List<UserVerify> userVerifies = userVerifyMapper.selectByExample(example);
            UserVerify userVerify = userVerifies.get(0);
            iVerify.sentEmail(member.getUsername(),userVerify.getUuid(),member.getEmail());
        }

        userLoginResponse.setAddress(member.getAddress());
        userLoginResponse.setBalance(member.getBalance());
        userLoginResponse.setDescription(member.getDescription());
        userLoginResponse.setEmail(member.getEmail());
        userLoginResponse.setFile(member.getFile());
        userLoginResponse.setId(member.getId());
        userLoginResponse.setPhone(member.getPhone());
        userLoginResponse.setPoints(member.getPoints());
        userLoginResponse.setSex(member.getSex());
        userLoginResponse.setState(member.getState());
        userLoginResponse.setUsername(member.getUsername());
        userLoginResponse.setCode(SysRetCodeConstants.SUCCESS.getCode());
        userLoginResponse.setMsg(SysRetCodeConstants.SUCCESS.getMessage());

        //将用户信息加密为token
        JwtTokenUtils tokenUtilsBuilder = JwtTokenUtils.builder().msg(JSON.toJSONString(userLoginResponse)).build();
        String token = tokenUtilsBuilder.creatJwtToken();
        userLoginResponse.setToken(token);
        return userLoginResponse;
    }
}
